package com.heima.behavior.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.service.IApUnlikeService;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.behavior.pojos.ApUnlike;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.utils.common.UserThreadLocal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * <p>
 * 文章不喜欢表 前端控制器
 * </p>
 *
 * @author fkn
 * @since 2024-08-21
 */
@RestController
public class ApUnlikeController {

    @Resource
    private IApUnlikeService unlikeService;

    private final static short UNLIKE = 0;

    @PostMapping("/api/v1/un_likes_behavior")
    public ResponseResult unlike(@RequestBody @Valid UnLikesBehaviorDto dto){
        ApUnlike apUnlike = ApUnlike.builder().articleId(dto.getArticleId()).userId(UserThreadLocal.get())
                .unlikeTime(LocalDateTime.now()).build();
        if (UNLIKE == dto.getType()){
            unlikeService.save(apUnlike);
        }else {
            LambdaQueryWrapper<ApUnlike> condition = new LambdaQueryWrapper<ApUnlike>()
                    .eq(ApUnlike::getUserId, UserThreadLocal.get())
                    .eq(ApUnlike::getArticleId, dto.getArticleId());
            unlikeService.remove(condition);
        }
        return ResponseResult.okResult(null);
    }
}
